package skat;

import skat.collections.Hand;

public class GameFinder {

	private Hand hand;
	private int jackCount;
	private int jackVal;
	private boolean jackUpper;

	private int[] games = new int[6];

	public GameFinder(Hand h) {
		hand = h;
		analyzeHand();
	}

	public void analyzeHand() {
		analyzeJacks();
		analyzeColors();
	}

	private void analyzeColors() {
		games = new int[6];
		for (Card c : hand) {
			switch (c.rank()) {
				case 3 : // Unter
					for (int i = 1; i < 6; i++)
						games[i] += 2 * (c.suit() + 1);
					break;
				case 7 : // Ass
					for (int i = 1; i < 6; i++)
						games[i] += 4;
					break;
				case 6 : // 10
					for (int i = 1; i < 6; i++)
						games[i] += 4;
					games[0] += 2;
					break;
				case 5 :
					games[5] += 2;
					games[c.suit() + 1] += 2;
					break;
				case 4 :
					games[c.suit() + 1] += 2;
					break;
				default :
					games[0] += (6 - c.rank());
					games[c.suit() + 1] += (c.rank() == 2 ? 2 : 1);
					break;
			}
		}
	}

	private void analyzeJacks() {
		boolean[] jack = new boolean[4];
		for (Card c : hand) {
			if (c.rank() == 3) {
				jack[c.suit()] = true;
			}
			jackCount++;
		}

		// mit x: +1..4 // ohne x: -1..4
		jackVal = 1;
		for (int i = 2; i >= 0; i--) {
			if (jack[i] == jack[3]) {
				jackVal++;
			} else
				break;
		}
		jackUpper = jack[3];
	}

	public int getJackCount() {
		return jackCount;
	}

	public int getJackVal() {
		return jackVal;
	}

	public boolean isJackUpper() {
		return jackUpper;
	}

	public float grandRate() {
		return games[5] / 48.0f;
	}

	public float acornRate() {
		return games[4] / 48.0f;
	}
	
	public float greenRate() {
		return games[3] / 48.0f;
	}
	
	public float redRate() {
		return games[2] / 48.0f;
	}
	
	public float schellRate() {
		return games[1] / 48.0f;
	}
	
	public float nullRate() {
		return games[0] / 48.0f;
	}

}
